// pages/me/editAddress.js
const app = new getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    address: [],
    region: [],
    customItem: '全部'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options.id);
    this.getCurAddress(options.id);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindRegionChange: function (e) {
    this.setData({
      region: e.detail.value
    });
  },
  // 获取当前地址的信息
  getCurAddress: function (id) {
    var that = this;
    wx.request({
      url: app.globalData.serverUrl + 'getCurAddress',
      method: 'GET',
      data: {
        id: id,
        openid: app.globalData.openid
      },
      success: function (e) {
        // console.log(e);
        console.log(e.data);
        that.setData({
          address: e.data,
          region: e.data.region
        });
      }
    })
  },
  // 删除地址
  delAddress: function (e) {
    console.log(e);
    wx.showModal({
      title: '提示',
      content: '确定要删除此收货地址',
      cancelColor: '#fc5871',
      confirmColor: '#fc5871',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.serverUrl + 'delAddress',
            method: 'GET',
            data: {
              id: e.currentTarget.dataset.id,
              openid: app.globalData.openid
            },
            success: function (e) {
              console.log(e);
              if (e.data.status == 1) {
                wx.showToast({
                  title: '删除成功',
                  icon: 'success',
                  duration: 2000,
                  success: function (res) {
                    // console.log(res);
                    setTimeout(function () {
                      wx.navigateBack({
                        delta: 1
                      })
                    }, 2000);
                  }
                })
              } else {
                wx.showModal({
                  title: '提示',
                  showCancel: false,
                  content: e.data.content,
                  confirmColor: '#fc5871'
                })
              }
            }
          })
        }
      }
    })
  },
  // 表单提交
  formSubmit: function (e) {
    // console.log(e);
    if (e.detail.value.name == this.data.address.name &&
      e.detail.value.phone == this.data.address.phone &&
      e.detail.value.address == this.data.address.address &&
      this.data.region[0] == this.data.address.province &&
      this.data.region[1] == this.data.address.city &&
      this.data.region[2] == this.data.address.area) {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '未修改收货地址！',
        confirmColor: '#fc5871'
      })
    } else {
      var that = this;
      console.log(e);
      wx.request({
        url: app.globalData.serverUrl + 'editReceiveAddress',
        method: 'GET',
        data: {
          id: e.detail.value.id,
          name: e.detail.value.name,
          phone: e.detail.value.phone,
          address: e.detail.value.address,
          province: that.data.region[0],
          city: that.data.region[1],
          area: that.data.region[2]
        },
        success: function (e) {
          // console.log(e);
          if (e.data.status == 1) {
            wx.showToast({
              title: '编辑成功',
              icon: 'success',
              duration: 2000,
              success: function (res) {
                // console.log(res);
                setTimeout(function () {
                  wx.navigateBack({
                    delta: 1
                  })
                }, 2000);
              }
            })
          } else {
            wx.showModal({
              title: '提示',
              showCancel: false,
              content: '编辑失败'
            })
          }
        }
      })
    }
  }
})